package com.example.yijian.edit.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.yijian.MSApplication;
import com.example.yijian.R;
import com.example.yijian.edit.background.SpaceItemDecoration;
import com.example.yijian.edit.watermarker.EffectItemData;
import com.example.yijian.utils.ScreenUtils;
import com.example.yijian.utils.SystemUtils;
import com.example.yijian.utils.datainfo.TimelineData;
import com.example.yijian.utils.datainfo.VideoFx;

import java.util.ArrayList;
import java.util.List;

import static android.widget.LinearLayout.HORIZONTAL;

public class EffectView extends RelativeLayout {
    private Context mContext;
    private View rootView;
    private LinearLayout llBlur;
    private SeekBar seekBlur;
    private LinearLayout llMosaic;
    private SeekBar seekLevel;
    private SeekBar seekNum;
    private RecyclerView mRecyclerView;
    private ImageView ivFinish;
    private EffectAdapter mEffectAdapter;
    private List<EffectItemData> mData = new ArrayList<>();

    private OnItemClickListener mOnItemClickListener;
    private OnEffectSeekBarListener mOnEffectSeekBarListener;

    private int mSelectPosition = -1;

    public int getSelectPosition() {
        return mSelectPosition;
    }

    public void refreshSelectPosition() {
        int position = mSelectPosition;
        mSelectPosition = -1;
        if (mEffectAdapter != null) {
            mEffectAdapter.notifyItemChanged(position);
        }
        llBlur.setVisibility(View.GONE);
        llMosaic.setVisibility(View.GONE);
    }

    public EffectView(Context context) {
        super(context);
        mContext = context;
        this.init();
    }

    public EffectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        this.init();
    }

    public EffectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        this.init();
    }

    private void init() {
        rootView = LayoutInflater.from(mContext).inflate(R.layout.view_effect, this);
        llBlur = rootView.findViewById(R.id.linear_blur);
        seekBlur = rootView.findViewById(R.id.view_seek_bar_level_blur);
        llMosaic = rootView.findViewById(R.id.linear_mosaic);
        seekLevel = rootView.findViewById(R.id.view_seek_bar_level);
        seekNum = rootView.findViewById(R.id.view_seek_bar_num);
        mRecyclerView = rootView.findViewById(R.id.recycler_effect);
        ivFinish = rootView.findViewById(R.id.iv_effect_finish);
        initData();
        initListener();
        rootView.setOnClickListener(v -> {
        });
        ivFinish.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rootView.setVisibility(GONE);
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onDismiss();
                }
            }
        });
    }

    private void initListener() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(0, ScreenUtils.dip2px(mContext, 29)));
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mEffectAdapter = new EffectAdapter());


        seekLevel.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float level = progress / 100.0f;
                if (mOnEffectSeekBarListener != null) {
                    mOnEffectSeekBarListener.onViewLevelChange(level);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        seekNum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float num = progress / 1000.0f;
                if (mOnEffectSeekBarListener != null) {
                    mOnEffectSeekBarListener.onViewNumberChange(num);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        seekBlur.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float level = progress * 0.64f;
                if (mOnEffectSeekBarListener != null) {
                    mOnEffectSeekBarListener.onViewBlurChange(level);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    private void initData() {
        mData.clear();

        EffectItemData effectItemData = new EffectItemData();
        effectItemData.setName(getResources().getString(R.string.sub_menu_edit_mask_none));
        effectItemData.setImagePath(R.mipmap.sub_menu_icon_edit_mask_none);
        mData.add(effectItemData);

        effectItemData = new EffectItemData();
        effectItemData.setType(EffectItemData.TYPE_MOSAIC);
        effectItemData.setName(getResources().getString(R.string.effect_name_mosaic));
        effectItemData.setImagePath(R.mipmap.icon_effect_masaike);
        mData.add(effectItemData);

        effectItemData = new EffectItemData();
        effectItemData.setType(EffectItemData.TYPE_BLUR);
        effectItemData.setName(getResources().getString(R.string.effect_name_blur));
        effectItemData.setImagePath(R.mipmap.icon_effect_blur);
        mData.add(effectItemData);
    }


    class EffectAdapter extends RecyclerView.Adapter<EffectAdapter.MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(
                    mContext).inflate(R.layout.item_effect_recycleview, parent,
                    false));
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
            EffectItemData effectItemData = mData.get(position);

            if (effectItemData != null) {
                int imagePath = effectItemData.getImagePath();
                Glide.with(mContext).load(imagePath).into(holder.imageView);
                holder.mName.setText(effectItemData.getName());
            }

            holder.itemWatermarkCover.setVisibility(position == 0 ? View.INVISIBLE : mSelectPosition == position ? View.VISIBLE : View.INVISIBLE);

            holder.imageView.setOnClickListener(v -> {
                mEffectAdapter.notifyItemChanged(mSelectPosition);
                mSelectPosition = position;
                mEffectAdapter.notifyItemChanged(position);
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onEffectClick(position, mData.get(position));
                }

                if (position == 0) {
                    llBlur.setVisibility(View.GONE);
                    llMosaic.setVisibility(View.GONE);
                } else if (position == 1) {
                    llBlur.setVisibility(View.GONE);
                    llMosaic.setVisibility(View.VISIBLE);
                } else {
                    llBlur.setVisibility(View.VISIBLE);
                    llMosaic.setVisibility(View.GONE);
                }

                updateSeekBarStatus();
            });
        }

        private void updateSeekBarStatus() {
            VideoFx videoFx = TimelineData.instance().getVideoFx();
            if (videoFx == null) {
                return;
            }
            seekLevel.setProgress((int) (videoFx.getIntensity() * 100));
            seekNum.setProgress((int) (videoFx.getUnitSize() * 1000.0f));
            seekBlur.setProgress((int) (videoFx.getRadius() / 0.64));
        }


        @Override
        public int getItemCount() {
            return mData.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            ImageView itemWatermarkCover;
            TextView mName;

            MyViewHolder(View view) {
                super(view);
                imageView = (ImageView) view.findViewById(R.id.item_watermark_image);
                itemWatermarkCover = (ImageView) view.findViewById(R.id.item_watermark_cover);
                mName = view.findViewById(R.id.tv_name);

            }
        }
    }

    public void setOnEffectSeekBarListener(OnEffectSeekBarListener onEffectSeekBarListener) {
        this.mOnEffectSeekBarListener = onEffectSeekBarListener;
    }

    public interface OnEffectSeekBarListener {

        /**
         * 马赛克变动回调
         * Mosaic change callback
         *
         * @param value
         */
        void onViewLevelChange(float value);


        /**
         * 马赛克数量变动回调
         * Mosaic number change callback
         *
         * @param value
         */
        void onViewNumberChange(float value);


        /**
         * 模糊程度变动回调
         * Degree of blur change callback
         *
         * @param value
         */
        void onViewBlurChange(float value);

    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onEffectClick(int position, EffectItemData effectItemData);

        void onDismiss();
    }

}
